/*
 * Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.huawei.cloudphone.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import com.huawei.cloudphone.common.CASLog;

/**
 * CASService
 */
public class CASService extends Service {

    /**
     * TAG:CASService
     */
    private static final String TAG = "CASService";

    /**
     * channel id
     */
    private static final String CHANNEL_ID_STRING = "CASService";

    /**
     * cas processor
     */
    private CasProcessor mCasProcessor = null;

    /**
     * notify id
     */
    private int mNotifyID = 100;

    @Override
    public IBinder onBind(Intent intent) {
        CASLog.i(TAG, "onBind");
        mCasProcessor = new CasProcessor();
        return mCasProcessor;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        CASLog.i(TAG, "onUnbind");
        mCasProcessor = null;
        return super.onUnbind(intent);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int ret = super.onStartCommand(intent, flags, startId);
        CASLog.i(TAG, "onStartCommand");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_STRING, "CASService", NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);
            Notification notification = new Notification.Builder(getApplicationContext(), CHANNEL_ID_STRING).build();
            startForeground(mNotifyID, notification);
        }
        return ret;
    }

    @Override
    public void onCreate() {
        CASLog.i(TAG, "onCreate");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        CASLog.i(TAG, "onDestroy");
        super.onDestroy();
    }
}
